<%@page contentType="text/html;charset=UTF-8" %>
<%@page import="net.sf.json.JSONObject"%>
<%@page import="net.sf.json.JSONArray"%>
<%@page import="f.admin.entity.*"%>
<%@page import="f.admin.service.*"%>
<%@page import="java.text.*" %>
<%@page import="java.util.*" %> 


<%

	request.setCharacterEncoding("utf-8");
	String action = request.getParameter("action");
	DictionaryModuleService dictionaryModuleService = new DictionaryModuleService();
	DictionaryObjectService dictionaryObjectService = new DictionaryObjectService();
	if(action.equals("addDictModule")) {
		try {
			String name = request.getParameter("name");
			DictionaryModule dictModule = new DictionaryModule();
			dictModule.setName(name);
			dictionaryModuleService.add(dictModule);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
			e.printStackTrace();
		}
	} else if(action.equals("getDictModuleList")) {
		List<DictionaryModule> dictModuleList = dictionaryModuleService.listAll();
		JSONArray dictModulejsonArr = dictionaryModuleService.listToJsonArray(dictModuleList);
		response.getWriter().write(dictModulejsonArr.toString());
	} else if(action.equals("editDictModule")) {
		try {
			String id = request.getParameter("id");
			String name = request.getParameter("name");
			DictionaryModule dictModule = dictionaryModuleService.getById(id);
			dictModule.setName(name);
			dictionaryModuleService.update(dictModule);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
			e.printStackTrace();
		}
	} else if(action.equals("getDictModuleById")) {
		String id = request.getParameter("id");
		DictionaryModule dictModule = dictionaryModuleService.getById(id);
		response.getWriter().write(dictModule.getJsonString());
	} else if(action.equals("deleteDictModule")) {
		try {
			String id = request.getParameter("id");
			List<DictionaryObject> dictList = dictionaryObjectService.getByQueryParam(new QueryParam("dictionaryModuleId", "=", id));
			for(DictionaryObject dict : dictList) {
				dictionaryObjectService.delete(dict.getId());
			}
			dictionaryModuleService.delete(id);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
			e.printStackTrace();
		}
	} else if(action.equals("addDict")) {
		try {
			String moduleId = request.getParameter("dictionaryModuleId");
			String name = request.getParameter("name");
			String aliasName = request.getParameter("aliasName");
			String description = request.getParameter("description");
			DictionaryModule dictModule = new DictionaryModule();
			dictModule.setId(moduleId);
			DictionaryObject dict = new DictionaryObject(name, aliasName, description, dictModule);
			dictionaryObjectService.add(dict);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
			e.printStackTrace();
		}
	} else if(action.equals("getDictList")) {
		String moduleId = request.getParameter("dictionaryModuleId");
		int currentPage = Integer.parseInt(request.getParameter("currentPage"));
		int pageSize = Integer.parseInt(request.getParameter("pageSize"));
		if(moduleId.equals("")) {
			response.getWriter().write("");
			return;
		}
		List<DictionaryObject> dictList = dictionaryObjectService.listByPage(currentPage, pageSize, new QueryParam("dictionaryModuleId", "=", moduleId));
		JSONArray dictJsonArr = dictionaryObjectService.listToJsonArray(dictList);
		JSONObject jsonObj = new JSONObject();
		jsonObj.put("total", dictionaryObjectService.getCount());
		jsonObj.put("rows", dictJsonArr);
		response.getWriter().write(jsonObj.toString());
	} else if(action.equals("getDictById")) {
		String id = request.getParameter("id");
		DictionaryObject dict = dictionaryObjectService.getById(id);
		response.getWriter().write(dict.getJsonString());
	} else if(action.equals("editDict")) {
		try {
			String id = request.getParameter("id");
			String name = request.getParameter("name");
			String aliasName = request.getParameter("aliasName");
			String description = request.getParameter("description");
			DictionaryObject dict = dictionaryObjectService.getById(id);
			dict.setName(name);
			dict.setAliasName(aliasName);
			dict.setDescription(description);
			dictionaryObjectService.update(dict);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
			e.printStackTrace();
		}
	} else if(action.equals("deleteDict")) {
		try {
			String dictId = request.getParameter("dictId");
			dictionaryObjectService.delete(dictId);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
		}
	} else if(action.equals("addDictItem")) {
		try {
			String dictId = request.getParameter("dictId");
			String text = request.getParameter("text");
			dictionaryObjectService.addItem(dictId, text);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
		}
	} else if(action.equals("getDictItemList")) {
		String dictId = request.getParameter("dictId");
		if(!dictId.equals("")) {
			DictionaryObject dict = dictionaryObjectService.getById(dictId);
			response.getWriter().write(dict.getValue() == null ? "" : dict.getValue());
		} else 
			response.getWriter().write("");
	} else if(action.equals("editDictItem")) {
		try {
			String dictId = request.getParameter("dictId");
			String value = request.getParameter("value");
			String text = request.getParameter("text");
			DictionaryObject dict = dictionaryObjectService.getById(dictId);
			JSONArray jsonArr = JSONArray.fromObject(dict.getValue());
			JSONArray editJsonArr = new JSONArray(); 
			for(int i = 0; i < jsonArr.size(); i++) {
				JSONObject jsonObj = jsonArr.getJSONObject(i);
				if(jsonObj.getString("value").equals(value)) {
					jsonObj.put("text", text);
				}
				editJsonArr.add(jsonObj);
			}
			dict.setValue(editJsonArr.toString());
			dictionaryObjectService.update(dict);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
		}
	} else if(action.equals("deleteDictItem")) {
		try {
			String dictId = request.getParameter("dictId");
			String value = request.getParameter("value");
			DictionaryObject dict = dictionaryObjectService.getById(dictId);
			JSONArray jsonArr = JSONArray.fromObject(dict.getValue());
			JSONArray editJsonArr = new JSONArray(); 
			for(int i = 0; i < jsonArr.size(); i++) {
				JSONObject jsonObj = jsonArr.getJSONObject(i);
				if(jsonObj.getString("value").equals(value)) {
					continue;
				}
				editJsonArr.add(jsonObj);
			}
			dict.setValue(editJsonArr.toString());
			dictionaryObjectService.update(dict);
			response.getWriter().write("success");
		} catch(Exception e) {
			response.getWriter().write("failure");
		}
	}
%>